using Gameplay.PVE.Entity;
using UnityEngine;

namespace Gameplay.PVE
{
    public class UnitTouch : MonoBehaviour
    {
        private UnitBase unit;

        public void Init(UnitBase unit)
        {
            this.unit = unit;
        }

        public UnitBase GetUnit()
        {
            return unit;
        }
    }

    public class ControlComponent : IComponent, ITransmit
    {
        private UnitBase unit;

        private UnitTouch unitTouch;
        private BoxCollider collider;

        private GameObject dragEffect;
        private GameObject pointerEffect;
        private GameObject selectionEffect;
        private GameObject rangeEffect;
        private int rangeEffectId;
        private int selectionId;

        //操作控件
        public void OnReset()
        {
            colliderInited = false;
            GameObject.Destroy(collider);
            GameObject.Destroy(unitTouch);
        }
        void IComponent.OnInitialize(UnitBase unit)
        {
            this.unit = unit;
        }

        void IComponent.OnUpdate()
        {

        }

        void IComponent.OnBirth()
        {
            if (PveManager.Instance.rpgStart)
            {
                InitCollider();
            }
        }

        public void SetCollider(Vector3 center, Vector3 size, bool isTrigger)
        {
            if (collider!=null)
            {
                collider.center = center;
                collider.size = size;
                collider.isTrigger = isTrigger;
            }
        }
        void IComponent.OnDead()
        {
            if (rangeEffect != null)
            {
                PveResourceManager.RecoverResource(rangeEffect,PveResourceManager.EPveResType.Pointer,rangeEffectId);
                rangeEffect = null;
            }
            if (selectionEffect != null)
            {
                PveResourceManager.RecoverResource(selectionEffect, PveResourceManager.EPveResType.Pointer, selectionId);
                selectionEffect = null;
            }

            if (collider)
            {
                collider.enabled = false;
            }
            unitTouch.enabled = false;
            //GameObject.Destroy(collider);
            //GameObject.Destroy(unitTouch);
        }

        void ITransmit.OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            if (type == ETransmitType.OnPointerLock)
            {
                if (pointerEffect == null)
                {
                    pointerEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer, 2);
                    pointerEffect.transform.SetParent(unit.gameObject.transform);
                    pointerEffect.transform.localPosition = new Vector3(0,0.01f,0);
                    pointerEffect.SetActive(true);
                }
            }
            else if (type == ETransmitType.OnPointerUnLock)
            {
                if (pointerEffect != null)
                {
                    PveResourceManager.RecoverResource(pointerEffect,PveResourceManager.EPveResType.Pointer,2);
                    pointerEffect = null;
                }
            }
            else if (type == ETransmitType.OnSelectBySkill)
            {
                if (pointerEffect == null)
                {
                    pointerEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer, 2);
                    pointerEffect.transform.SetParent(unit.gameObject.transform);
                    pointerEffect.transform.localPosition = new Vector3(0,0.01f,0);
                    pointerEffect.SetActive(true);
                }
                //unit.gameObject.SetLayerRecursively(PveUtils.blackScreenLayer);
            }
            else if (type == ETransmitType.OnUnSelectBySkill)
            {
                if (pointerEffect != null)
                {
                    PveResourceManager.RecoverResource(pointerEffect,PveResourceManager.EPveResType.Pointer,2);
                    pointerEffect = null;
                }
                //unit.gameObject.SetLayerRecursively(PveUtils.pveUnitLayer);
            }
            else if (type == ETransmitType.OnDrag)
            {
                /*if (dragEffect == null)
                {
                    dragEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer, 1);
                    dragEffect.transform.SetParent(unit.gameObject.transform);
                    dragEffect.transform.localPosition = new Vector3(0,0f,0);
                    dragEffect.SetActive(true);
                }*/
            }
            else if (type == ETransmitType.OnCancelDrag)
            {
                if (dragEffect != null)
                {
                    PveResourceManager.RecoverResource(dragEffect,PveResourceManager.EPveResType.Pointer,1);
                    dragEffect = null;
                }
            }
            else if (type == ETransmitType.OnSelected)
            {
                if (dragEffect == null && !unit.Data.isDead)
                {
                    dragEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer, 1);
                    dragEffect.transform.SetParent(unit.gameObject.transform);
                    dragEffect.transform.localPosition = new Vector3(0,0.01f,0);
                    dragEffect.SetActive(true);
                }
            }
            else if (type == ETransmitType.OnUnSelected)
            {
                if (dragEffect != null)
                {
                    PveResourceManager.RecoverResource(dragEffect,PveResourceManager.EPveResType.Pointer,1);
                    dragEffect = null;
                }
            }
            else if (type == ETransmitType.StageStart)
            {
                unit.Data.isDraggable = false;
                if (PveManager.Instance.isInPve)
                {
                    if (selectionEffect != null)
                    {
                        PveResourceManager.RecoverResource(selectionEffect, PveResourceManager.EPveResType.Pointer, selectionId);
                        selectionEffect = null;
                    }
                    if (rangeEffect != null)
                    {
                        PveResourceManager.RecoverResource(rangeEffect, PveResourceManager.EPveResType.Pointer, rangeEffectId);
                        rangeEffect = null;
                    }
                }
            }
            else if (type == ETransmitType.StageToNext)
            {
                unit.Data.isDraggable = false;
                if (PveManager.Instance.isInPve)
                {
                    if (selectionEffect != null)
                    {
                        PveResourceManager.RecoverResource(selectionEffect, PveResourceManager.EPveResType.Pointer, selectionId);
                        selectionEffect = null;
                    }
                    if (rangeEffect != null)
                    {
                        PveResourceManager.RecoverResource(rangeEffect, PveResourceManager.EPveResType.Pointer, rangeEffectId);
                        rangeEffect = null;
                    }
                }
            }
            else if (type == ETransmitType.RpgStageToNext)
            {
                if (selectionEffect != null)
                {
                    PveResourceManager.RecoverResource(selectionEffect, PveResourceManager.EPveResType.Pointer, selectionId);
                    selectionEffect = null;
                }
                if (rangeEffect != null)
                {
                    PveResourceManager.RecoverResource(rangeEffect, PveResourceManager.EPveResType.Pointer, rangeEffectId);
                    rangeEffect = null;
                }
            }
            else if (type == ETransmitType.ShowAttackRange)
            {
                if (unit.Data.isDead)
                {
                    return;
                }
                if (rangeEffect == null)
                {
                    rangeEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer,
                        rangeEffectId);
                    rangeEffect.transform.SetParent(unit.gameObject.transform);
                    rangeEffect.transform.localPosition = new Vector3(0, 0.01f, 0);
                    rangeEffect.transform.localScale = new Vector3(1, 1, 1) * unit.Data.attackRange;
                    rangeEffect.SetActive(true);
                }
            }
            else if (type == ETransmitType.HideAttackRange)
            {
                if (rangeEffect != null)
                {
                    PveResourceManager.RecoverResource(rangeEffect, PveResourceManager.EPveResType.Pointer, rangeEffectId);
                    rangeEffect = null;
                }
            }
            else if (type == ETransmitType.RPGStart)
            {
                if (unit.Data.team == 0)
                {
                    unit.Data.isDraggable = true;
                }
                selectionId = 0;
                rangeEffectId = 6;
                InitCollider();
                /*if (unit.Data.team == 0)
                {
                    if (selectionEffect == null)
                    {
                        selectionId = unit.Data.isManualMoving ? 4 : 5;
                        selectionEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer, selectionId);
                        selectionEffect.transform.SetParent(unit.gameObject.transform);
                        selectionEffect.transform.localPosition = new Vector3(0,0.01f,0);
                        selectionEffect.SetActive(true);
                    }

                    if (unit.Data.isManualMoving)
                    {
                        if (rangeEffect == null)
                        {
                            rangeEffect = PveResourceManager.GetResource(PveResourceManager.EPveResType.Pointer,
                                rangeEffectId);
                            rangeEffect.transform.SetParent(unit.gameObject.transform);
                            rangeEffect.transform.localPosition = new Vector3(0, 0.01f, 0);
                            rangeEffect.transform.localScale = new Vector3(1, 1, 1) * unit.Data.config.rpgAttackRange;
                            rangeEffect.SetActive(true);
                        }
                    }
                }*/
            }
            else if (type == ETransmitType.Reborn)
            {
                unitTouch.enabled = true;
                collider.enabled = true;
            }
            /*else if (type == ETransmitType.OnSelected)
            {
                PveManager.Instance.CameraFocus(unit);
            }
            else if (type == ETransmitType.OnUnSelected)
            {

            }*/
        }

        private bool colliderInited = false;

        private void InitCollider()
        {
            if (colliderInited)
            {
                return;
            }
            colliderInited = true;
            collider = unit.gameObject.GetComponentInChildren<BoxCollider>();
            if (collider == null)
            {
                collider = unit.gameObject.AddComponent<BoxCollider>();
                collider.size = unit.Data.colliderSize;
                collider.center = unit.Data.colliderCenter;
            }
            else
            {
                var size = collider.size;
                var center = collider.center;
                var position = collider.gameObject.transform.localPosition;
                float scale = collider.gameObject.transform.localScale.x;
                var q = Quaternion.FromToRotation(Vector3.forward, collider.gameObject.transform.forward);
                var newCenter = q  * (center * scale + position);
                var newSize = q * size * scale;
                newSize = new Vector3(Mathf.Abs(newSize.x),Mathf.Abs(newSize.y),Mathf.Abs(newSize.z));
                collider.enabled = false;
                collider = unit.gameObject.AddComponent<BoxCollider>();
                /*if (isFlip)
                {
                    collider.size = size * scale + new Vector3(0.2f,0.2f,1f);
                }
                else*/
                {
                    collider.size = newSize + new Vector3(0.2f,1,0.2f);
                }
                collider.center = newCenter; //center * scale + position;
                unit.Data.colliderSize = collider.size;
                unit.Data.colliderCenter = newCenter;
            }

            unitTouch = unit.gameObject.AddComponent<UnitTouch>();
            unitTouch.Init(unit);
        }
    }
}
